package com.guldan.auth.common.response;

import com.guldan.core.response.ResponseJsonAspect;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HeaderWithResponseJsonAspect extends ResponseJsonAspect {

    @Override
    public void before(JoinPoint jp) throws IOException {
        super.before(jp);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        String reqParamsString = (String) request.getAttribute(reqParamsStringKey);
        String authCode = request.getHeader("User-Code");
        reqParamsString += ",with User-Code:" + StringUtils.trimToEmpty(authCode);
        request.setAttribute(reqParamsStringKey, reqParamsString);
    }

}
